﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Threading;
using System.ComponentModel;
using System.Text;
using Communicate;
using BaseDll;
namespace OtherDevice
{
    public class TcpIp_Base
    {
        public string strName = "TCPIP";
        public TcpIp_Base(string strName)
        {
            this.strName = strName;
        }

        public Form_ControlBase controlFrom = null;
        public TcpIpParamter paramter = new TcpIpParamter();
        public TcpLink link = null;
        private string str_LastMeg = "";
        public Queue<string> m_messageQueue = new Queue<string>();
        public delegate void showMsg(string str);
        public event showMsg evenShowMsg = null;
        public int Init(TcpIpParamter paramter)
        {
            try
            {
                if (paramter.str_IP地址 == "")
                    return 0;
                this.paramter = paramter;
                link = new TcpLink(paramter.n_Index, paramter.str_Name, paramter.str_IP地址, paramter.n_Port, 5000, paramter.str_接收结束符);
                link.RecvStringMessageEvent += (object sender, AsyTcpSocketEventArgs e) =>
                {
                    if (evenShowMsg != null)
                        evenShowMsg(e.Message);
                };
                return linkEtherNetControl();
            }
            catch (Exception)
            {
                return -1;
            }
        }
        public virtual int linkEtherNetControl()
        {
            return 0;
        }
        public void clearMessageQueue()
        {
            str_LastMeg = "";
            m_messageQueue.Clear();
        }
        public int OutMessageQueue(out string mes)
        {
            mes = "";
            if (m_messageQueue.Count > 0)
            {
                mes = m_messageQueue.Dequeue();
                return 0;
            }
            else
                return -1;
        }

        public void InMessageQueue(string mes)
        {
            m_messageQueue.Enqueue(mes);
            str_LastMeg = mes;
            DeviceDataCollect.GetInstance().Function_OtherDeviceLog(strName, "接收", mes);
        }

        public void Send(string cmd)  //向指定客户端发送指令
        {
            string strMsg = paramter.str_发送结束符;
            if (paramter.str_发送结束符 == "CRLF")
            {
                strMsg = "\r\n";
            }
            else if (paramter.str_发送结束符 == "CR")
            {
                strMsg = "\r";
            }
            else if (paramter.str_发送结束符 == "LF")
            {
                strMsg = "\n";
            }
            else if (paramter.str_发送结束符 == "无")
            {
                strMsg = "";
            }
            link.WriteString(cmd+ strMsg);
            DeviceDataCollect.GetInstance().Function_OtherDeviceLog(strName, "发送", cmd + strMsg);
        }
        public string Function_GetLaseMeg()
        {
            return str_LastMeg;
        }

    }
}